package expenager.project.two;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class preferences extends Activity{
	//DBCurrentSetting dbSetting = new DBCurrentSetting(this);
	
	Button btnCurrency, btnPaymentMethod, btnAddPaymentMethod, btnDeletePaymentMethod,
	btnIncomes, btnAddIncomes, btnDeleteIncomes,
	btnabout;
	int currIndex, paymentMethodIndex, incomeIndex;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preferences);
		
		 btnabout = (Button) findViewById(R.id.btnabout);
		 btnabout.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent i = new Intent(getApplicationContext(), about.class);
					startActivity(i);
				}
			});
		
		btnCurrency = (Button) findViewById(R.id.btncurrency);
		btnCurrency.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				CurrencyDialog();
			}
		});
		
		btnPaymentMethod = (Button) findViewById(R.id.btnpayment);
		btnPaymentMethod.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				PaymentMethodDialog();
			}
		});
		
		btnAddPaymentMethod = (Button) findViewById(R.id.btnpaymentplus);
		btnAddPaymentMethod.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				addPaymentMethodDialog();
			}
		});
		
		btnDeletePaymentMethod = (Button) findViewById(R.id.btnpaymentmin);
		btnDeletePaymentMethod.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				deletePaymentMethodDialog();
			}
		});
		
		btnIncomes = (Button) findViewById(R.id.btncategories);
		btnIncomes.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				IncomesDialog();
			}
		});
		
		btnAddIncomes = (Button) findViewById(R.id.btncategoryplus);
		btnAddIncomes.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				addIncomeDialog();
			}
		});
		
		btnDeleteIncomes = (Button) findViewById(R.id.btncategorymin);
		btnDeleteIncomes.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				deleteIncomeDialog();
			}
		});
		
		
		}
	
	public void CurrencyDialog(){
		currIndex = 0;
		DBAdapter db = new DBAdapter(this);
		db.open();
		Cursor c = db.getCurrency();
		String curr1 = c.getString(0).toString();
		c.moveToNext();
		String curr2 = c.getString(0).toString();
		c.moveToNext();
		String curr3 = c.getString(0).toString();
		c.moveToNext();
		String curr4 = c.getString(0).toString();
		c.moveToNext();
		db.close();
		
		final String curr[] = {curr1, curr2, curr3, curr4};
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				setCurrency(curr[currIndex]);
				Toast.makeText(getApplicationContext(), "Selected Currency: "+curr[currIndex], Toast.LENGTH_SHORT).show();
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				
			}
		});
		
		dialog.setTitle("Set Currency");
		dialog.setSingleChoiceItems(curr, 0, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface arg0, int arg1) {
				//Toast.makeText(getApplicationContext(), Integer.toString(arg1), Toast.LENGTH_SHORT).show();
				currIndex = arg1;
			}
		});
		
		dialog.show();
	}
	
	public void setCurrency(String sign) {
		//dbSetting.setCurrencySign(sign);
		//Cursor c = dbSetting.getCurrencySign();
		//c.moveToLast();
		//ADD CURRENCY TO SETTING DB
		DBAdapter db = new DBAdapter(this);
		db.open();
		db.setCurrencySetting(sign);
		db.close();
	}
	
	public void PaymentMethodDialog() {
		final DBAdapter db = new DBAdapter(this);
		final List<String> selectedPaymentMethod = new ArrayList<String>();
		
		db.open();
		
		Cursor c = db.getPaymentMethod();
		final String paymentMethod[] = new String[c.getCount()];
		for(int i=0;i<c.getCount();i++) {
			paymentMethod[i] = c.getString(0).toString();			
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
		
		db.close();
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		
		dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				if(selectedPaymentMethod.isEmpty()==true){
					Toast.makeText(getApplicationContext(), "Selection Aborted\n\nPlease select at least 1 item", Toast.LENGTH_SHORT).show();
					
				}
				else {
					String arrPaymentMethod[] = new String[selectedPaymentMethod.size()];
					for(int i=0;i<selectedPaymentMethod.size();i++){
						arrPaymentMethod[i] = selectedPaymentMethod.get(i);;
					}
					Toast.makeText(getApplicationContext(), Integer.toString(arrPaymentMethod.length)+" Payment Method type(s) will be displayed on Transactions Page", Toast.LENGTH_SHORT).show();
					db.open();
					db.setPaymentMethodSetting(arrPaymentMethod);
					db.close();
				}
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				
			}
		});
		
		dialog.setTitle("Set Payment Method");
		dialog.setMultiChoiceItems(paymentMethod, null, new DialogInterface.OnMultiChoiceClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
				if(arg2==true){
					selectedPaymentMethod.add(paymentMethod[arg1]);
				}
				
				if(arg2==false){
					selectedPaymentMethod.remove(paymentMethod[arg1]);
				}
			}
		});
				
		dialog.show();
	}
	
	public void addPaymentMethodDialog() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("Add");
		dialog.setMessage("Input your new Payment Method:");
		
		final EditText textAdd = new EditText(this);
		textAdd.setInputType(InputType.TYPE_CLASS_TEXT);
		dialog.setView(textAdd);
		
		dialog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				addPaymentMethod(textAdd.getText().toString());
				Toast.makeText(getApplicationContext(), "Added Payment Method: "+textAdd.getText().toString(), Toast.LENGTH_SHORT).show();
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});

		dialog.show();	
	}
	
	public void deletePaymentMethodDialog() {
		paymentMethodIndex=0;		
		DBAdapter db = new DBAdapter(this);
		db.open();
		
		Cursor c = db.getPaymentMethod();
		final String paymentMethod[] = new String[c.getCount()];
		for(int i=0;i<c.getCount();i++) {
			paymentMethod[i] = c.getString(0).toString();			
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
				
		db.close();
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		
		dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				deletePaymentMethod(paymentMethod[paymentMethodIndex]);
				Toast.makeText(getApplicationContext(), "Deleted Payment Method: "+paymentMethod[paymentMethodIndex], Toast.LENGTH_SHORT).show();
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				
			}
		});
		
		dialog.setTitle("Delete");
		dialog.setSingleChoiceItems(paymentMethod, 0, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				paymentMethodIndex=arg1;
			}
		});
				
		dialog.show();
	}
	
	public void addPaymentMethod(String addPaymentMethod) {
		DBAdapter db = new DBAdapter(this);
		db.open();
		db.addPaymentMethod(addPaymentMethod);
		db.close();
	}
	
	public void deletePaymentMethod(String deletePaymentMethod) {
		DBAdapter db = new DBAdapter(this);
		db.open();
		db.deletePaymentMethod(deletePaymentMethod);
		db.close();
	}
	
	public void IncomesDialog() {
			final DBAdapter db = new DBAdapter(this);
			final List<String> selectedIncome = new ArrayList<String>();
			
			db.open();
			
			Cursor c = db.getIncome();
			final String income[] = new String[c.getCount()];
			for(int i=0;i<c.getCount();i++) {
				income[i] = c.getString(0).toString();			
				
				if(i!=(c.getCount()-1)) {
					c.moveToNext();
					}
			}
					
			db.close();
			
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			
			dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface arg0, int arg1) {
					if(selectedIncome.isEmpty()==true){
						Toast.makeText(getApplicationContext(), "Selection Aborted\n\nPlease select at least 1 item", Toast.LENGTH_SHORT).show();
						
					}
					else {
						String arrIncome[] = new String[selectedIncome.size()];
						for(int i=0;i<selectedIncome.size();i++){
							arrIncome[i] = selectedIncome.get(i);;
						}
						Toast.makeText(getApplicationContext(), Integer.toString(arrIncome.length)+" Income type(s) will be displayed on Transactions Page", Toast.LENGTH_SHORT).show();
						db.open();
						db.setIncomeSetting(arrIncome);
						db.close();
					}
				}
			});
			
			dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface arg0, int arg1) {
					
				}
			});
			
			dialog.setTitle("Set Incomes");
			dialog.setMultiChoiceItems(income, null, new DialogInterface.OnMultiChoiceClickListener() {
				
				public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
					if(arg2==true){
						selectedIncome.add(income[arg1]);
					}
					
					if(arg2==false){
						selectedIncome.remove(income[arg1]);
					}
				}
			});
					
			dialog.show();
		}
	
	public void addIncomeDialog() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("Add");
		dialog.setMessage("Input your new Income:");
		
		final EditText textAdd = new EditText(this);
		textAdd.setInputType(InputType.TYPE_CLASS_TEXT);
		dialog.setView(textAdd);
		
		dialog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				addIncome(textAdd.getText().toString());
				Toast.makeText(getApplicationContext(), "Added Income: "+textAdd.getText().toString(), Toast.LENGTH_SHORT).show();
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});

		dialog.show();	
	}
	
	public void deleteIncomeDialog() {
		incomeIndex=0;		
		DBAdapter db = new DBAdapter(this);
		db.open();
		
		Cursor c = db.getIncome();
		final String income[] = new String[c.getCount()];
		for(int i=0;i<c.getCount();i++) {
			income[i] = c.getString(0).toString();			
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
				
		db.close();
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		
		dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				deleteIncome(income[incomeIndex]);
				Toast.makeText(getApplicationContext(), "Deleted Income: "+income[incomeIndex], Toast.LENGTH_SHORT).show();
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				
			}
		});
		
		dialog.setTitle("Delete");
		dialog.setSingleChoiceItems(income, 0, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				incomeIndex=arg1;
			}
		});
				
		dialog.show();
	}
	
	public void addIncome(String addIncome) {
		DBAdapter db = new DBAdapter(this);
		db.open();
		db.addIncome(addIncome);
		db.close();
	}
	
	public void deleteIncome(String deleteIncome) {
		DBAdapter db = new DBAdapter(this);
		db.open();
		db.deleteIncome(deleteIncome);
		db.close();
	}
	
	}
